#!/usr/bin/env node

const utils = require('./utils')
const core = require('./index')
const config = require('./config')()
const app = require('../core/build/app')
const packageJson = core.IsInstalled ? require('../../../../package.json') : require('../package.json')

const commander = require('commander')
commander
  .option('-f, --force', '清除目录再创建')
  .parse(process.argv);

const command = commander.args[0]

if (!command) return

utils.brand()

switch (command) {
  case 'fix':
    require('../bin/fix')
    break;
  case 'colors':
    require('../bin/colors')
    break
  case 'coder':
    require('../bin/coder')
    break;
  case 'svg':
    require('../bin/svg')
    break;
  case 'theme':
    // eslint-disable-next-line no-case-declarations
    const type = commander.args[1]
    if (!type) {
      require('../bin/el-theme')
      require('../bin/ui-theme')
      return
    }

    if (type === 'el') {
      require('../bin/el-theme')
    }
    if (type === 'ui') {
      require('../bin/ui-theme')
    }
    if (type === 'prod') {
      require('../bin/prod-theme')
    }

    break;
  case 'ready':
    require('../bin/ready')
    break;
  case 'preview':
    require('../bin/preview')
    break;
  case 'server':
    require('../bin/server')
    break;
  case 'route':
    // eslint-disable-next-line no-case-declarations
    const routeAction = commander.args[1]
    // eslint-disable-next-line no-case-declarations
    const routeApp = commander.args[2]
    // eslint-disable-next-line no-case-declarations
    const routesBuilder = require('./build/routes')
    // eslint-disable-next-line no-case-declarations
    let viewPath = ''
    // eslint-disable-next-line no-case-declarations
    let outFile = ''
    // eslint-disable-next-line no-case-declarations
    let autoRoutes = true
    if (routeApp) {
      const apps = packageJson.apps || {}
      const app = apps[routeApp]
      if (app) {
        viewPath = utils.join(core.AppsPath, routeApp, 'views')
        outFile = utils.join(core.TempPath, 'routes', `${routeApp}.js`)
        autoRoutes = app.autoRoutes
      } else {
        utils.log(`应用${routeApp}不存在`)
      }
    } else {
      viewPath = core.ViewsPath
      outFile = core.OutputRoutesFile
      autoRoutes = config.autoRoutes
    }
    if (routeAction === 'watch') {
      routesBuilder.watch(
        viewPath,
        outFile,
        !autoRoutes
      )
    }
    if (routeAction === 'build') {
      routesBuilder.build(
        viewPath,
        outFile,
        !autoRoutes
      )
    }
    break;
  case 'bridge':
    require('../bin/bridge')
    break;
  case 'app':
    // 操作类型
    // eslint-disable-next-line no-case-declarations
    const action = commander.args[1]
    // 站点名称
    // eslint-disable-next-line no-case-declarations
    const appName = commander.args[2]
    // 端口号
    // eslint-disable-next-line no-case-declarations
    const port = commander.args[3] || '8001'

    switch (action) {
      case 'add':
        if (!appName) {
          utils.log('需要指定应用名称', 'warning')
          return false
        }
        app.add(appName, parseInt(port))
        break;
      case 'remove':
        if (!appName) {
          utils.log('需要指定应用名称', 'warning')
          return false
        }
        app.remove(appName)
        break;
      case 'build':
        app.build()
        break;
      default:
        utils.log('缺少操作名称', 'warning')
        break;
    }

    break

}



